<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html>
<head>
{include file='header.tpl' JS=$smarty.const.BAOGG_JS CSS=$smarty.const.BAOGG_CSS}        
        
        <script type="text/javascript"> 
           {literal}
          	Ext.onReady(function(){
			Ext.get("check_all_resource") && Ext.get("check_all_resource").on("click",function(){
				Ext.select("input[type=checkbox][name^=resource_row_id]").each(function(el,c,idx){
					el.dom.checked=Ext.get("check_all_resource").dom.checked;
					Ext.select("input[name='resource_op_code"+el.dom.name.replace("resource_row_id","")+"[]']").each(function(subel,subc,subidx){
						subel.dom.checked=el.dom.checked;
					});
				})
			});
			Ext.select("input[type=checkbox][name^=resource_row_id]").each(function(el,c,idx){
				
				el.on("click",function(e,t,o){
					//console.info("input[name='resource_op_code"+t.name.replace("resource_row_id","")+"[]']");
					Ext.select("input[name='resource_op_code"+t.name.replace("resource_row_id","")+"[]']").each(function(subel,subc,subidx){
						subel.dom.checked=t.checked;
					});
				});				
			});
          		Ext.get("submit") && Ext.get("submit").on("click",function(){
	          		Ext.Ajax.request({
	          		    form: 'form1',
	          		  	success: function(form, action) {		
	          		  		//console.info(action);		
	          		  	  var result=Ext.decode(form.responseText);
	          		  	  if(result.success){
		          		  		 top.top.Ext.example.msg('{/literal}{$LANG.save}{literal}', '{/literal}{$LANG.save_ok}{literal}');
			          		  	 parent.Ext.getCmp("resource_role_win").close();
	          		  	  }else{
		          		  		 Ext.MessageBox.show({
			          		           title: '{/literal}{$LANG.save_fail}{literal}',
			          		           msg: result.msg,
			          		           buttons: Ext.MessageBox.OK,
			          		           icon:  Ext.MessageBox.ERROR
			          		       });
	          		  	  }					   				
	     			    },
	     			   failure: function(form, action) {
	    					switch (action.failureType) {
	    					    case Ext.form.Action.CLIENT_INVALID:
	    						top.Ext.example.msg('{/literal}{$LANG.save_fail}{literal}', "Form fields may not be submitted with invalid values");
	    						break;
	    					    case Ext.form.Action.CONNECT_FAILURE:
	    						top.Ext.example.msg('{/literal}{$LANG.save_fail}{literal}', "Ajax communication failed");
	    						break;
	    					    case Ext.form.Action.SERVER_INVALID:
	    					       top.Ext.example.msg('{/literal}{$LANG.save_fail}{literal}', action.result.msg);
	    					       break;
	    					    default:
	    					      top.Ext.example.msg('{/literal}{$LANG.save_fail}{literal}', action.result.msg);
	    					      break;
	    				       }
	    				   }
	          		});

          		});
          		 
          	});
          	{/literal}
          </script>	
</head>
<body>       
<form method="post" id="form1" name="form1" action="{$smarty.const.BAOGG_BASE_URL}system/resource/role/">

	<div id="toolbar">
		{if !$gridShow}
			<input type="submit" value="{$LANG.save}"  />		
		{else}
			<input type="button" value="{$LANG.save}" id="submit" name="submit" />
		{/if}
	</div>

	<hr />
	<div id="form">
		{if !$gridShow}
		<div id="div_role">
			<select name="role_id" id="role_id">
				{foreach item=role from=$roles}
					{if $role_id==$role.role_id}
						<option value="{$role.role_id}" selected>{$role.role_name}</option>
					{else}
						<option value="{$role.role_id}">{$role.role_name}</option>
					{/if}
				{/foreach}
			</select>
		  
			<select name="resource_id" id="resource_id">
				{foreach item=resource from=$resources}
					{if $resource_id==$resource.resource_id}
						<option value="{$resource.resource_id}" selected>{$resource.resource_name}</option>
					{else}
						<option value="{$resource.resource_id}">{$resource.resource_name}</option>
					{/if}
				{/foreach}
			</select>
		</div>
	
		{else}
		
		<table class="grid">
			<thead>
				<tr>
				<th><input type="checkbox" id="check_all_resource" /></th>
				
				<th>Resouce</th>
				<th>Op</th>
				</tr>
			</thead>
			<tbody>
			{foreach name=outer item=resource from=$resources}
			<tr>
				<td>
				{if $resource.is_resourced}				
				<input type="checkbox" name="resource_row_id[{$resource.key_col}]" 
				{if isset($permission[$resource.key_col])}
					checked 
				{/if}				
				 />
				 {/if}
				 </td>
				<td> {$resource.show_col}</td>
				<td>
				{if $resource.is_oped}
				{foreach name=op item=operation from=$operations}
					<input type="checkbox" name="resource_op_code[{$resource.key_col}][]" value="{$operation.resource_op_code}"
					{if isset($permission[$resource.key_col]) and  $permission[$resource.key_col].resource_op_code|strpos:$operation.resource_op_code!==false }
						checked 
					{/if}
					/><lable>{$operation.resource_op_name}</lable> &nbsp; 
				{/foreach}
				{/if}
				</td>	
			</tr>
			{/foreach}
			</tbody>
		</table>
		<input type="hidden" name="role_id" value="{$role_id}" />
		<input type="hidden" name="resource_id" value="{$resource_id}" />
		{/if}
	</div>

</form>
</body>
</html>